﻿Shader "Dan/UI/UI_Target"
{
    Properties
    {
     _Color ("Color", Color) = (0.07843138,0.3921569,0.7843137,1)
     _MainTex ("MainTex", 2D) = "white" {}
     _Speed ("Speed", Float ) = 4
     _Amplitude ("Amplitude", Range(0, 1)) = 0.1543558
     _Emission ("Emission", Float ) = 1
     [MaterialToggle] _UseBG ("UseBG", Float ) = 0
     _BG_Density ("BG_Density", Float ) = 32
     _BG_Opacity ("BG_Opacity", Range(0, 1)) = 0.75
     

    }
    
    SubShader
    {

        Tags { "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent" }
        LOD 100    
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off 
 
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            #include "../ShaderLibs/UIFX.cginc"
            #include "../ShaderLibs/UVOperator.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            
            struct v2f
            {
                float4 vertex : SV_POSITION;    
                float2 uv : TEXCOORD0;
                
            };

             float4 _Color;
             sampler2D _MainTex; 
             float4 _MainTex_ST;
             float _Speed;
             float _Amplitude;
             float _Emission;
             float _BG_Density;
             fixed _UseBG;
             float _BG_Opacity;

            v2f vert ( appdata v )
            {
                
                v2f o;
                UNITY_INITIALIZE_OUTPUT(v2f ,o);
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;        
                return o;
            }
            
            
            
            


            fixed4 frag (v2f i ) : SV_Target
            {
                float2 uv = i.uv;
                uv = TRANSFORM_TEX(uv, _MainTex);
                float3 finalColor = (_Color.rgb*_Emission);
                
                float4 mainTex = tex2D(_MainTex,uv);
                
                float2 rd = float2(_Time.y,_Time.y)*0.01;
                float Shinny = Flick(rd);
                
                
                //标记的采样和运动
                float targetShape ;
                TargetCorner(_Speed,_Amplitude,uv,_MainTex,targetShape);

                float spotGrid = ((1 - distance(float2(0.5,0.5),frac((uv*_BG_Density))))*5-4);
                float bg =saturate(spotGrid)*(1-targetShape)*_BG_Opacity;
                
                float useBG = lerp( 0, bg, _UseBG );

                float channelR = (mainTex.r+Shinny)*mainTex.r;

                return fixed4(finalColor,channelR+useBG+targetShape);
                
                
                
            }
            ENDCG
        }
    }
    
    
    
}
